<?php 
/**
 * @see Epic_View_Abstract
 */
require_once 'Epic/View/Abstract.php';

/**
 * Block class process given to the block (block) html
 *
 * @category   Epic
 * @package    Epic_View
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_View_Block extends Epic_View_Abstract
{
	/**
     * Template hint
     * @var boolean
     */
    private static $_templateHints;
    
    /**
     * Block hint
     * @var boolean
     */
    private static $_blockHints;
    
    /**
     * Request object
     * @var Epic_Controller_Request_Abstract
     */
    private $_request = null;
   
    /**
     * Epic_View_Abstract object
     * @var array
     */
    private $_views = array();

    /**
     * Pagination object
     * @var Epic_Paginator_Pagination
     */
    protected $_pagination = null;
    
    /**
     * 
     * @param unknown_type $key
     */
    public function getFormData($key)
    {
    	return Epic::getFormData($key);
    }
    
    /**
     * Retrieve request object
     *
     * @return Epic_Controller_Request_Abstract
     */
    public function getRequest()
    {
    	if(is_null($this->_request)) {
    		$this->_request = Epic_Controller_Front::getInstance()->getRequest();
    	}
    	return $this->_request;
    }
    
    /**
     * Create Pagination
     * 
     * @param int $total record retrieve from database
     */
    public function createPagination($total)
    {
    	$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
    	$this->_pagination = new Epic_Paginator_Pagination();
		$this->_pagination->setTotalItem($total)
						  ->setCurrentPage($currentPage)
						  ->createPagination();
    }
    
    /**
     * Retrive Pagination object
     *
     * @return Epic_Paginator_Pagination
     */
    public function getPagination() 
    {
    	return $this->_pagination;
    }
    
    /**
     * Render pagination html source
     * 
     * @param string $file name file used pagination
     */
    public function renderPaginationHtml($file)
    {
    	include(TEMPLATE_DIR . $file);
    }
    
    /**
     * Add an object view
     * 
     * @param Epic_View_Abstract $view
     */
    public function addView(Epic_View_Abstract $view)
    {
        if (!in_array($view, $this->_views, true)) {
            $this->_views[] = $view; 
        }
        return $this;
    }

    /**
     * Remove a view object
     * 
     * @param Epic_View_Abstract $view
     */
    public function removeView(Epic_View_Abstract $view)
    {
        if (in_array($view, $this->_views, true)) {
            $views = array();
            foreach ($this->_views as $_view) {
                if ($_view !== $view) {
                    $views[] = $_view;
                }
            }
            $this->_views = $views;
        }
        return $this;
    }
    
    /**
     * Retrive the template hint from config.ini
     * 
     * @return boolean
     */
    public function getTemplateHints()
    {
        if (is_null(self::$_templateHints)) {
        	$config = Epic::getRegistry()->get('config');
            self::$_templateHints = $config->getConfig('debug/template_hints');
            self::$_blockHints = $config->getConfig('debug/block_hints');
        }
        return self::$_templateHints;
    }
    
	/**
     * Render view template
     *
     * @throws Epic_View_Exception if not specify path to a file
     * @return string
     */
    public function render()
    {
        if (is_null($this->getTemplate())) {
        	throw new Epic_View_Exception('Not specify path to a file');
        }
        extract($this->_viewVars);
        ob_start();
        
        if ($this->getTemplateHints()) {
            echo '<div style="position:relative; border:1px dotted red; margin:6px 2px; padding:18px 2px 2px 2px; zoom:1;"><div style="position:absolute; left:0; top:0; padding:2px 5px; background:red; color:white; font:normal 11px Arial; text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'" onmouseout="this.style.zIndex=\'998\'" title="'.$this->getTemplate().'">'.$this->getTemplate().'</div>';
            if (self::$_blockHints) {
                $thisClass = get_class($this);
                echo '<div style="position:absolute; right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial; text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'" onmouseout="this.style.zIndex=\'998\'" title="'.$thisClass.'">'.$thisClass.'</div>';
            }
        }
        
        $innerView = '';
        if (!empty($this->_views)) {
            foreach ($this->_views as $view) {
                $innerView .= $view->render();
            }
            echo $innerView;
        }
        
        include($this->_template);
        
        if ($this->getTemplateHints()) {
            echo '</div>';
        }
        
        return ob_get_clean();
    }
}